package controller

import (
	"apitestAfterGo/common"
	"apitestAfterGo/domain"
	"apitestAfterGo/dto"
	"apitestAfterGo/service"
	"apitestAfterGo/utils"
	"github.com/kataras/iris/v12/context"
	"github.com/kataras/iris/v12/mvc"
)

type InterfaceController struct {
}

func (i *InterfaceController) BeforeActivation(b mvc.BeforeActivation) {
	b.Handle("POST", "/getAllInterFaceByPage", "GetAllInterFaceByPage")
	b.Handle("POST", "/addInterface", "AddInterface")
	b.Handle("DELETE", "/deleteInterFaceById/{id:int}", "DeleteInterFaceById")
	b.Handle("PUT", "/updateInterfaceById", "UpdateInterfaceById")
	b.Handle("GET", "/findInterfaceById/{id:int}", "FindInterfaceById")
	b.Handle("GET", "/findInterfaceBySystemId/{id:int}", "FindInterfaceBySystemId")
}

func (i *InterfaceController) GetAllInterFaceByPage(dto dto.InterfacePageInputDto) common.Result {
	interfaces, count := service.InterFaceServiceImpl.GetAllInterfaceByPage(dto)
	pages := utils.GetPages(count, dto.PageSize)
	page := domain.Page{
		Pages:   pages,
		Total:   count,
		Current: dto.Current,
		Size:    dto.PageSize,
		Records: interfaces,
	}
	return utils.Success(page, "获取当前系统接口成功")
}

func (i *InterfaceController) AddInterface(context *context.Context, in domain.Interface) common.Result {
	username := utils.GetJwtParameter(context, "username").(string)
	in.UpdateBy = username
	in.CreateBy = username
	flag := service.InterFaceServiceImpl.AddInterface(in)
	return utils.Success(flag, "添加系统接口成功")
}

func (i *InterfaceController) DeleteInterFaceById(id int) common.Result {
	flag := service.InterFaceServiceImpl.DeleteInterFaceById(id)
	return utils.Success(flag, "删除系统接口成功")
}

func (i *InterfaceController) UpdateInterfaceById(context *context.Context, in domain.Interface) common.Result {
	username := utils.GetJwtParameter(context, "username").(string)
	in.UpdateBy = username
	flag := service.InterFaceServiceImpl.UpdateInterfaceById(in)
	return utils.Success(flag, "更新系统接口成功")
}

func (i *InterfaceController) FindInterfaceById(id int) common.Result {
	interFace := service.InterFaceServiceImpl.FindInterfaceById(id)
	return utils.Success(interFace, "获取接口信息成功")
}

func (i *InterfaceController) FindInterfaceBySystemId(id int) common.Result {
	interFaces := service.InterFaceServiceImpl.FindInterfaceBySystemId(id)
	return utils.Success(interFaces, "获取当前系统接口成功")
}
